const Router = require("koa-router");
const fs = require("fs");
const path = require("path");
const router = new Router();
const dataPath = path.join(__dirname, "../../public/data.json");

/**
 * @route GET api/indexRouter/text
 * @desc  测试接口地址
 * @access 接口是公开的
 */
router.get("/text", async (ctx) => {
  ctx.body = {
    msg: "路由",
  };
});
/**
 * @route POST api/indexRouter/getData
 * @desc  测试接口地址
 * @access 接口是公开的
 */
router.post("/getData", async (ctx) => {
  let data = fs.readFileSync(dataPath, "utf-8");
  console.log(data);
  ctx.body = {
    msg: "路由",
    data,
  };
});
/**
 * @route POST api/indexRouter/addData
 * @desc  测试接口地址
 * @access 接口是公开的
 */
// 添加接口
router.post("/addData", async (ctx) => {
  console.log("进来了。。。。。。。。。。。。。。。。。。。");
  let data = JSON.parse(fs.readFileSync(dataPath, "utf-8"));
  let data_1 = await addData(ctx, data);
  console.log(data_1);
  fs.writeFileSync(dataPath, JSON.stringify(data_1), "utf-8");
  ctx.body = {
    mag: "找到了",
    data: data_1,
  };
  // 把查找要添加到那个数据的children的方法封装起来
  function addData(ctx, data) {
    let index = data.findIndex((item, index) => {
      return item.key == ctx.request.body.key;
    });
    console.log(index);
    // 如果找到了
    if (index !== -1) {
      // 添加到这条数据的cheildren里边
      console.log(data[index].children);
      if (!data[index].children) {
        console.log("1111111111111111111111");
        data[index].children = [];
        console.log(data[index]);
        data[index].children.push(ctx.request.body.newData);
      } else {
        data[index].children.push(ctx.request.body.newData);
      }
      return data;
    } else {
      // 如果没有找到递归查找
      data.forEach((item, index) => {
        if (!item.children) {
          return;
        } else {
          addData(ctx, item.children);
        }
      });
    }
    return data;
  }
});
// 删除接口
router.post("/remData", async (ctx) => {
  let data = JSON.parse(fs.readFileSync(dataPath, "utf-8"));
  console.log(ctx.request.body.key);
  let data_2 = await remData(ctx, data);
  fs.writeFileSync(dataPath, JSON.stringify(data_2), "utf-8");
  ctx.body = {
    data,
  };
  function remData(ctx, data) {
    let index = data.findIndex((item, index) => {
      return item.key == ctx.request.body.key;
    });
    console.log(index);
    // 如果找到了
    if (index !== -1) {
      // 直接删除这条数据
      data.splice(index, 1);
      return data;
    } else {
      // 如果没有找到递归查找
      data.forEach((item, index) => {
        if (!item.children) {
          return;
        } else {
          remData(ctx, item.children);
          if (item.children.length == 0) {
            delete item.children;
          }
        }
      });
    }
    return data;
  }
});
// 敏感非敏感接口
router.post("/selectData", async (ctx) => {
  let data = JSON.parse(fs.readFileSync(dataPath, "utf-8"));
  console.log(ctx.request.body.value);
  let data_3 = await selectData(ctx, data);
  console.log(data_3);
  fs.writeFileSync(dataPath, JSON.stringify(data_3), "utf-8");
  ctx.body = {
    data: data_3,
  };
  function selectData(ctx, data) {
    let index = data.findIndex((item, index) => {
      return item.key == ctx.request.body.key;
    });
    console.log(index);
    // 如果找到了
    if (index !== -1) {
      // 把这条数据的mustLength改为对应的Boolean值
      // mustLength 控制
      // jack 非敏感(true)
      // lucy 敏感(false)
      console.log(ctx.request.body.value);
      if (ctx.request.body.value == "jack") {
        console.log("进来啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦");
        data[index].mustLength = true;
      } else if (ctx.request.body.value == "lucy") {
        data[index].mustLength = false;
      }
      return data;
    } else {
      // 如果没有找到递归查找
      data.forEach((item, index) => {
        if (!item.children) {
          return;
        } else {
          selectData(ctx, item.children);
        }
      });
    }
    return data;
  }
});
// 点击表格中input修改表格内容接口
router.post("/xiuData", async (ctx) => {
  let data = JSON.parse(fs.readFileSync(dataPath, "utf-8"));
  console.log(ctx.request.body);
  let data_4 = await xiusData(ctx, data);
  fs.writeFileSync(dataPath, JSON.stringify(data_4), "utf-8");
  ctx.body = {
    data: data_4,
  };
  function xiusData(ctx, data) {
    let index = data.findIndex((item, index) => {
      console.log(item.key, ctx.request.body.key);
      return item.key == ctx.request.body.key;
    });
    console.log(index);
    // 如果找到了
    if (index !== -1) {
      // 把这条数据改为修改之后的数据
      data[index] = ctx.request.body;
      return data;
    } else {
      // 如果没有找到递归查找
      data.forEach((item, index) => {
        if (!item.children) {
          return;
        } else {
          xiusData(ctx, item.children);
        }
      });
    }
    return data;
  }
});
// 添加一条数据接口
router.post("/addsData", async (ctx) => {
  let data = JSON.parse(fs.readFileSync(dataPath, "utf-8"));
  console.log(ctx.request.body);
  let obj = {
    key: Number(data[data.length - 1].key) + 1,
    parameterName: "",
    parameterType: "",
    parameterDescribe: "",
    seqNum: "",
    parameterInputPutputType: "",
    objType: "",
    mustField: "",
    default: "",
    mustLength: false,
  };
  data.push(obj);
  fs.writeFileSync(dataPath, JSON.stringify(data), "utf-8");
  ctx.body = {
    data,
  };
});
module.exports = router.routes();
